package cn.com.ejiakanghu.dto;

import cn.com.ejiakanghu.core.utils.ParamUtils;
import org.apache.commons.lang.StringUtils;
import org.linlinjava.litemall.db.domain.LitemallAddress;
import sun.swing.StringUIClientPropertyKey;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class EjiaUserAddressInfo {

    private int id;
    private long userId;
    private String province;
    private String city;
    private String village;
    private String address;
    private String userName;
    private String phone;
    private int isDefault;
    private int gender;
    //门牌号楼层信息
    private String detail;
    private String lng;
    private String lat;
    private String isHos;
    private String hosId;
    private String hosName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getVillage() {
        return village;
    }

    public void setVillage(String village) {
        this.village = village;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public int getIsDefault() {
        return isDefault;
    }

    public void setIsDefault(int isDefault) {
        this.isDefault = isDefault;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public String getDetail() {
        if(StringUtils.isBlank(this.detail)) {
            return "";
        }
        return detail;
    }

    public void setDetail(String detail) {
        this.detail = detail;
    }

    public String getLng() {
        return lng;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public Integer getIsHos() {
        if(StringUtils.isNotBlank(isHos) && StringUtils.isNumeric(isHos)) {
            return Integer.parseInt(isHos);
        }
        return 0;
    }

    public void setIsHos(String isHos) {
        this.isHos = isHos == null ? "0" : (StringUtils.isBlank(isHos.toString()) || !StringUtils.isNumeric(isHos.toString()) ? "0" : isHos.toString());
    }

    public Integer getHosId() {
        Integer rst = StringUtils.isBlank(hosId) || !StringUtils.isNumeric(hosId) ? 0 : Integer.parseInt(hosId);
        return rst;
    }

    public void setHosId(String hosId) {
        if(hosId == null) {
            this.hosId = "0";
            return;
        }
        if(StringUtils.isBlank(hosId.toString())) {
            this.hosId = "0";
            return;
        }
        if(!StringUtils.isNumeric(hosId.toString())) {
            this.hosId = "0";
            return;
        }
        this.hosId = hosId.toString();
    }

    public String getHosName() {
        return hosName;
    }

    public void setHosName(String hosName) {
        this.hosName = hosName;
    }

    /**
     * 将之前系统的地址转换为新的对象地址信息
     * @param litemallAddress
     * @return
     */
    public static EjiaUserAddressInfo getInstance(LitemallAddress litemallAddress) {
        if(litemallAddress == null) {
            return null;
        }
        EjiaUserAddressInfo userAddressInfo = new EjiaUserAddressInfo();
        userAddressInfo.setAddress(litemallAddress.getAddress());
        userAddressInfo.setUserId(litemallAddress.getUserId());
        userAddressInfo.setDetail(litemallAddress.getAddressDetail());
        userAddressInfo.setUserName(litemallAddress.getName());
        userAddressInfo.setId(litemallAddress.getId());
        userAddressInfo.setCity(litemallAddress.getCity());
        Boolean isDefault = litemallAddress.getIsDefault();
        userAddressInfo.setIsDefault(isDefault != null && isDefault ? 1 : 0);
        userAddressInfo.setPhone(litemallAddress.getTel());
        userAddressInfo.setProvince(litemallAddress.getProvince());
        userAddressInfo.setVillage(litemallAddress.getCounty());
        userAddressInfo.setLat(litemallAddress.getLat() == null ? "0" : litemallAddress.getLat().toString());
        userAddressInfo.setLng(litemallAddress.getLng() == null ? "0" : litemallAddress.getLng().toString());
        userAddressInfo.setGender(litemallAddress.getGender());
        userAddressInfo.setIsHos(litemallAddress.getIsHospital() == null ? "0" : litemallAddress.getIsHospital() + "");
        userAddressInfo.setHosId(litemallAddress.getHospitalId() == null ? "0" : litemallAddress.getHospitalId() + "");
        userAddressInfo.setHosName(litemallAddress.getHospitalName());
        return userAddressInfo;
    }
    public static List<EjiaUserAddressInfo> getListInstance(List<LitemallAddress> litemallAddressList) {
        List<EjiaUserAddressInfo> addressInfoList = new ArrayList<>();
        litemallAddressList.stream().forEach(addr->{
            addressInfoList.add(EjiaUserAddressInfo.getInstance(addr));
        });
        return addressInfoList;
    }
    public static LitemallAddress getInstanceLite(EjiaUserAddressInfo addressInfo) {
        LitemallAddress addr = new LitemallAddress();
        addr.setAddress(addressInfo.getAddress());
        addr.setAddressDetail(addressInfo.getDetail());
        addr.setUserId((int) addressInfo.getUserId());
        addr.setCity(addressInfo.getCity());
        addr.setCounty(addressInfo.getVillage());
        addr.setIsDefault(addressInfo.getIsDefault() == 1 ? true : false);
        addr.setName(addressInfo.getUserName());
        addr.setProvince(addressInfo.getProvince());
        addr.setTel(addressInfo.getPhone());
        addr.setId(addressInfo.getId());
        addr.setGender(addressInfo.getGender());
        String lat = addressInfo.getLat();
        if(StringUtils.isNotBlank(lat) && ParamUtils.isDecimal(lat)) {
            addr.setLat(new BigDecimal(lat));
        }
        String lng = addressInfo.getLng();
        if(StringUtils.isNotBlank(lng) && ParamUtils.isDecimal(lng)) {
            addr.setLng(new BigDecimal(lng));
        }
        addr.setIsHospital(addressInfo.getIsHos());
        addr.setHospitalId(addressInfo.getHosId());
        addr.setHospitalName(addressInfo.getHosName());
        return addr;
    }

    public static void main(String[] args) {
//        EjiaUserAddressInfo addr = new EjiaUserAddressInfo();
//        addr.setHosId("null");
        System.out.println("-------");
    }
}
